package com.jar.exercise.多线程.线程之间的通信;

/**
 * 使用两个线程打印 1-100。线程1, 线程2 交替打印
 */
public class ThreadCommunication3 {
	public static void main(String[] args) {
		PrintTest printTest = new PrintTest();
		new Thread(printTest).start();
		new Thread(printTest).start();
	}
}

class PrintTest implements Runnable {
	private int num = 100;

	@Override
	public void run() {
		while (true) {
			synchronized (this) {
				notify();
				if (num > 0) {
					try {
						System.out.println(Thread.currentThread().getName() + "：" + num--);
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} else {
					break;
				}
			}
		}
	}
}
